import requests
from datetime import datetime

def soTrain(myKeyword):

	headers = {
		'Accept': '*/*',
		'Accept-Encoding': 'gzip, deflate, br',
		'Accept-Language': 'zh-CN,zh;q=0.9',
		'Connection': 'keep-alive',
		'Host': 'search.12306.cn',
		'Origin': 'https://kyfw.12306.cn',
		'Referer': 'https://kyfw.12306.cn/otn/queryTrainInfo/init',
		'Sec-Fetch-Mode': 'cors',
		'Sec-Fetch-Site': 'same-site',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
	}

	now = datetime.now()
	mydatetime1 = now.strftime("%Y%m%d")
	mydatetime2 = now.strftime("%Y-%m-%d")

	

	url = 'https://search.12306.cn/search/v1/train/search?keyword={keyword}&date={date}'.format(keyword =myKeyword ,date =mydatetime1 )
	#print(url)
	html = requests.get(url,headers = headers)
	mydata = html.json()
	train_n =  mydata['data'][0]['train_no']

	url = 'https://kyfw.12306.cn/otn/queryTrainInfo/query?leftTicketDTO.train_no={train_no}&leftTicketDTO.train_date={train_date}&rand_code='.format(train_no = train_n, train_date = mydatetime2)


	headers = {
		'Accept': '*/*',
		'Accept-Encoding': 'gzip, deflate, br',
		'Accept-Language': 'zh-CN,zh;q=0.9',
		'Connection': 'keep-alive',
		'Cookie': 'JSESSIONID=2F501E774F2162FACC4D32B08894A960; BIGipServerotn=904921610.38945.0000; RAIL_EXPIRATION=1602648417986; RAIL_DEVICEID=oZvvOGVQuKTn5IXLyWNVV_YNQYIc7mRrTLRNUNosOA1CJ238CZEfTjfZN1EHIVZG5mRY4RCHrQjTaz_6N9mxkOAfZ0kAvN89EnPqHeX-pR-A3uy-fztYvuAL7qNY1u7jRQ1SssHhmlakt9rmrZf8H73TmWmag5yH; BIGipServerpool_passport=283378186.50215.0000; route=6f50b51faa11b987e576cdb301e545c4; _jc_save_fromDate=2020-10-11',
		'Host': 'kyfw.12306.cn',
		'Referer': 'https://kyfw.12306.cn/otn/queryTrainInfo/init',
		'Sec-Fetch-Mode': 'cors',
		'Sec-Fetch-Site': 'same-origin',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
		'X-Requested-With': 'XMLHttpRequest'
		}


	html = requests.get(url,headers = headers)
	return html.json()

if __name__ == "__main__":
	myKeyword = input('输入车号:')
	print(soTrain(myKeyword))
